package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.domain.AProduct;
import com.ruoyi.system.domain.vo.ComplaintVo;
import com.ruoyi.system.service.IAProductService;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Complaint;
import com.ruoyi.system.service.IComplaintService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 投诉Controller
 * 
 * @author 张颖
 * @date 2025-01-08
 */
@Controller
@RequestMapping("/system/complaint")
public class ComplaintController extends BaseController
{
    private String prefix = "system/complaint";

    @Autowired
    private IComplaintService complaintService;

    @Resource
    private IAProductService productService;

    @Resource
    private ISysUserService sysUserService;

    @RequiresPermissions("system:complaint:view")
    @GetMapping()
    public String complaint()
    {
        return prefix + "/complaint";
    }

    /**
     * 查询投诉列表
     */
    @RequiresPermissions("system:complaint:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Complaint complaint)
    {
        startPage();
        List<Complaint> list = complaintService.selectComplaintList(complaint);
        List<ComplaintVo> complaintVos = new ArrayList<>();
        list.forEach(item -> {
            ComplaintVo complaintVo = new ComplaintVo();
            BeanUtils.copyProperties(item, complaintVo);
            complaintVo.setUserName(sysUserService.selectUserById(item.getUserId()).getUserName());
            complaintVos.add(complaintVo);
        });
        return getDataTable(complaintVos);
    }

    /**
     * 导出投诉列表
     */
    @RequiresPermissions("system:complaint:export")
    @Log(title = "投诉", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Complaint complaint)
    {
        List<Complaint> list = complaintService.selectComplaintList(complaint);
        ExcelUtil<Complaint> util = new ExcelUtil<Complaint>(Complaint.class);
        return util.exportExcel(list, "投诉数据");
    }

    /**
     * 新增投诉
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("productList", productService.selectAProductList(new AProduct()));
        List<SysUser> sysUsers = sysUserService.selectUserList(new SysUser());
        mmap.put("userList", sysUserService.selectUserList(new SysUser()));
        return prefix + "/add";
    }

    /**
     * 新增保存投诉
     */
    @RequiresPermissions("system:complaint:add")
    @Log(title = "投诉", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Complaint complaint)
    {
        return toAjax(complaintService.insertComplaint(complaint));
    }

    /**
     * 修改投诉
     */
    @RequiresPermissions("system:complaint:edit")
    @GetMapping("/edit/{complaintId}")
    public String edit(@PathVariable("complaintId") Long complaintId, ModelMap mmap)
    {
        Complaint complaint = complaintService.selectComplaintByComplaintId(complaintId);
        mmap.put("complaint", complaint);
        mmap.put("productList", productService.selectAProductList(new AProduct()));
        mmap.put("userList", sysUserService.selectUserList(new SysUser()));
        return prefix + "/edit";
    }

    /**
     * 修改保存投诉
     */
    @RequiresPermissions("system:complaint:edit")
    @Log(title = "投诉", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Complaint complaint)
    {
        return toAjax(complaintService.updateComplaint(complaint));
    }

    /**
     * 删除投诉
     */
    @RequiresPermissions("system:complaint:remove")
    @Log(title = "投诉", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(complaintService.softDeleteComplaintByComplaintIds(ids));
    }
}
